package com.brillilab.service.core.topics.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.topics.TopicMemberMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.topics.TopicMember;
import com.brillilab.domain.vo.topics.TopicMemberVo;
import com.brillilab.service.core.topics.ITopicMemberService;

@Service
@Transactional
public class TopicMemberServiceImpl implements ITopicMemberService {

	@Resource
	private TopicMemberMapper topicMemberMapper;

	@Override
	public List<TopicMember> selectByLabMemberId(Long labMemberId) {
		return topicMemberMapper.selectList(new LambdaQueryWrapper<TopicMember>()
				.eq(TopicMember::getLabMemberId, labMemberId).eq(TopicMember::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public List<TopicMember> selectByInLabMemberIds(List<Long> labMemberIds) {
		if (CollectionUtils.isNotEmpty(labMemberIds)) {
			return topicMemberMapper
					.selectList(new LambdaQueryWrapper<TopicMember>().in(TopicMember::getLabMemberId, labMemberIds)
							.eq(TopicMember::getIsDelete, BoolEnum.FALSE.getValue()));
		}
		return new ArrayList<>(0);
	}

	@Override
	public int insertBatch(List<TopicMember> insertList) {
		if (CollectionUtils.isNotEmpty(insertList)) {
			return topicMemberMapper.insertBatch(insertList);
		}
		return 0;
	}
	
	@Override
	public void updateById(TopicMember topicMember) {
		topicMemberMapper.updateById(topicMember);
	}

	@Override
	public List<TopicMember> selectListByLabId(Long labId) {
		return topicMemberMapper.selectList(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getLabId, labId)
				.eq(TopicMember::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public List<TopicMemberVo> selectVoList(Long labId, Long topicId, Integer state) {
		return topicMemberMapper.selectVoList(labId, topicId, state);
	}

	@Override
	public List<TopicMember> selectListByTopicIds(List<Long> topicIds) {
		if (CollectionUtils.isNotEmpty(topicIds)) {
			return topicMemberMapper.selectList(new LambdaQueryWrapper<TopicMember>().in(TopicMember::getId, topicIds));
		}
		return new ArrayList<>(0);
	}

	@Override
	public boolean deleteExecutor(Long topicId) {
		return topicMemberMapper.delete(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getTopicId, topicId)
				.eq(TopicMember::getIsExecutor, BoolEnum.TRUE.getValue())) >= 0;
	}

	@Override
	public boolean deleteMembers(Long topicId) {
		return topicMemberMapper.delete(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getTopicId, topicId)
				.eq(TopicMember::getIsExecutor, BoolEnum.FALSE.getValue())) >= 0;
	}

	@Override
	public List<TopicMember> selectListByTopicId(Long topicId) {
		return topicMemberMapper.selectList(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getTopicId, topicId));
	}

	@Override
	public List<TopicMember> selectListByLabMemberId(Long labMemberId) {
		return topicMemberMapper
				.selectList(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getLabMemberId, labMemberId));
	}

	@Override
	public List<Long> getIdenticalTopicLabMemberIds(Long labMemberId) {
		return topicMemberMapper.getIdenticalTopicLabMemberIds(labMemberId);
	}

	@Override
	public List<Long> selectTopicIds(Long labId, Long labMemberId) {
		return topicMemberMapper.selectTopicIds(labId, labMemberId);
	}

	@Override
	public List<TopicMember> selectByLabMemberIdAndState(Long labMemberId, Integer state) {
		return topicMemberMapper.selectList(new LambdaQueryWrapper<TopicMember>()
				.eq(TopicMember::getLabMemberId, labMemberId).eq(TopicMember::getState, state));
	}

	@Override
	public boolean topicMemberAudit(Long topicMemberId, Integer isAdopt) {
		TopicMember member = topicMemberMapper.selectById(topicMemberId);
		Assert.notNull(member, "数据不存在");
		member.setState(isAdopt == 0 ? 2 : 1);
		return topicMemberMapper.updateById(member) == 1;
	}

	@Override
	public TopicMember getTopicMember(Long topicId, Long labMemberId) {
		return topicMemberMapper.selectOne(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getTopicId, topicId)
				.eq(TopicMember::getLabMemberId, labMemberId).eq(TopicMember::getIsDelete, BoolEnum.FALSE.getValue()));
	}
	
	@Override
	public TopicMember getTopicMainMember(Long topicId) {
		return topicMemberMapper.selectOne(new LambdaQueryWrapper<TopicMember>().eq(TopicMember::getTopicId, topicId)
				.eq(TopicMember::getIsExecutor, BoolEnum.TRUE.getValue()).eq(TopicMember::getIsDelete, BoolEnum.FALSE.getValue()));
	}

	@Override
	public boolean isMainExecutor(Long loginerId, Long labMemberId) {
		int count = topicMemberMapper.getTopicNum(loginerId, labMemberId);
		return count > 0;
	}
}
